package com.mud.mudcommune.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mud.mudcommune.R;

public class MainFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private static final MainFragment mainFragment = new MainFragment();
    private Fragment homeFragment, caseFragment, applyFragment, orderFragment, mineFragment;
    private RadioGroup radioGroup;
    private Fragment currentFragment;

    public static MainFragment getInstance() {
        return mainFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        radioGroup = (RadioGroup) view.findViewById(R.id.main_rg);
        homeFragment = HomePagerFragment.getInstance();
        caseFragment = CaseFragment.getInstance();
        orderFragment = OrderFragment.getInstance();
        applyFragment = ApplyFragment.getInstance();
        mineFragment = MineFragment.getInstance();
        if (!homeFragment.isAdded()) {
            getFragmentManager().beginTransaction().add(R.id.frame_layout, homeFragment).commit();
            currentFragment = homeFragment;
        }
        radioGroup.setOnCheckedChangeListener(this);
    }


    public void isAddOrShowFragment(Fragment fragment) {
        if (fragment == currentFragment) {
            return;
        }
        if (!fragment.isAdded()) {
            getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.frame_layout, fragment).commit();
        } else {
            getFragmentManager().beginTransaction().hide(currentFragment).show(fragment).commit();
        }
        currentFragment = fragment;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton button = (RadioButton) group.getChildAt(i);
            if (button.isChecked()) {
                button.setTextColor(getResources().getColor(R.color.subColor));
                switch (button.getId()) {
                    case R.id.rb_home:
                        isAddOrShowFragment(homeFragment);
                        break;
                    case R.id.rb_case:
                        isAddOrShowFragment(caseFragment);
                        break;
                    case R.id.rb_apply:
                        isAddOrShowFragment(applyFragment);
                        break;
                    case R.id.rb_order:
                        isAddOrShowFragment(orderFragment);
                        break;
                    case R.id.rb_mine:
                        isAddOrShowFragment(mineFragment);
                        break;
                }
            } else {
                button.setTextColor(getResources().getColor(R.color.black));
            }
        }
    }
}
